﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;

namespace ToWaDBFirst.Models
{
    /**
     * Membership Provider class that inherits from MembershipProvider to override certain functionality we need
     * for the project.
     **/
    public class ToWaMembershipProvider : MembershipProvider
    {

        /**
         * Class object to handle database operations.
         **/
        ToWaDBFirstDBEntities2 db = new ToWaDBFirstDBEntities2();

        public override string ApplicationName
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        /**
         * Method to change password. Currently not implemented.
         **/
        public override bool ChangePassword(string username, string oldPassword, string newPassword)
        {
            if (ValidateUser(username, oldPassword))
            {
                db.users.Single(r => r.username == username).password = newPassword;
                db.SaveChanges();
                return true;
            }
            return false;
        }

        /**
         * Method to create a user with a username, password, email.
         * As long as the username, password and email inputs aren't null or empty, and the username does not exist in the
         * database, the user is created in the database and the MembershipUser object is returned.
         **/
        public override MembershipUser CreateUser(string username, string password, string email, string passwordQuestion, string passwordAnswer, bool isApproved, object providerUserKey, out MembershipCreateStatus status)
        {
            if (string.IsNullOrEmpty(username))
            {
                status = MembershipCreateStatus.InvalidUserName;
                return null;
            }
            if (string.IsNullOrEmpty(password))
            {
                status = MembershipCreateStatus.InvalidPassword;
                return null;
            }
            if (string.IsNullOrEmpty(email))
            {
                status = MembershipCreateStatus.InvalidEmail;
                return null;
            }

            user user = null;
            try
            {
                user = db.users.Single(r => r.username == username);
            }
            catch { }

            if (user != null)
            {
                status = MembershipCreateStatus.DuplicateUserName;
                return null;
            }

            ToWaMembershipUser ToWaUser = new ToWaMembershipUser("ToWaMembershipProvider",
                    username,
                    null,
                    password,
                    null,
                    null,
                    isApproved,
                    false,
                    System.DateTime.Now,
                    System.DateTime.Now,
                    System.DateTime.Now,
                    System.DateTime.Now,
                    System.DateTime.Now
                    );

            user ownUser = new user { username = username, password = password, email = email };

            db.users.Add(ownUser);
            db.SaveChanges();

            status = MembershipCreateStatus.Success;

            return ToWaUser;

        }

        /**
         * Method to get the user from the database provided with a username.
         * If the username input is null or empty null is returned.
         * If a user is found a MembershipUser is created with the specified information found in the database
         * and is returned.
         **/
        public override MembershipUser GetUser(string username, bool userIsOnline)
        {
            if (string.IsNullOrEmpty(username))
            {
                return null;
            }

            user user = null;

            try
            {
                user = db.users.Single(r => r.username == username);
            }
            catch
            {

            }

            if (user != null)
            {
                ToWaMembershipUser memUser = new ToWaMembershipUser("ToWaMembershipProvider",
                    user.username,
                    null,
                    user.password,
                    null,
                    null,
                    true,
                    false,
                    System.DateTime.Now,
                    System.DateTime.Now,
                    System.DateTime.Now,
                    System.DateTime.Now,
                    System.DateTime.Now);

                memUser.ToWaId = user.id;
                memUser.ToWaUserName = user.username;

                return memUser;
            }
            return null;
        }

        /**
         * Method to get a specific user from the database with an email address.
         **/
        public override string GetUserNameByEmail(string email)
        {
            return db.users.Single(r => r.email == email).username;
        }

        /**
         * Method to validate a user with the provided username and password. If no user is found in the database
         * false is returned. If a user is found and the username and password correspond with the information in the
         * database true is returned.
         **/
        public override bool ValidateUser(string username, string password)
        {
            user user = null;
            try
            {
                user = db.users.Single(r => r.username == username);
            }
            catch { }
            if (user != null)
            {
                return (user.password == password);
            }
            return false;
        }

        /**
         * Unimplemented methods.
         **/
        public override bool ChangePasswordQuestionAndAnswer(string username, string password, string newPasswordQuestion, string newPasswordAnswer)
        {
            throw new NotImplementedException();
        }
        public override bool DeleteUser(string username, bool deleteAllRelatedData)
        {
            throw new NotImplementedException();
        }
        public override bool EnablePasswordReset
        {
            get { return false; }
        }
        public override bool EnablePasswordRetrieval
        {
            get { return false; }
        }
        public override MembershipUserCollection FindUsersByEmail(string emailToMatch, int pageIndex, int pageSize, out int totalRecords)
        {
            throw new NotImplementedException();
        }
        public override MembershipUserCollection FindUsersByName(string usernameToMatch, int pageIndex, int pageSize, out int totalRecords)
        {
            throw new NotImplementedException();
        }
        public override MembershipUserCollection GetAllUsers(int pageIndex, int pageSize, out int totalRecords)
        {
            throw new NotImplementedException();
        }
        public override int GetNumberOfUsersOnline()
        {
            throw new NotImplementedException();
        }
        public override string GetPassword(string username, string answer)
        {
            throw new NotImplementedException();
        }
        public override MembershipUser GetUser(object providerUserKey, bool userIsOnline)
        {
            throw new NotImplementedException();
        }
        public override int MaxInvalidPasswordAttempts
        {
            get { return 3; }
        }
        public override int MinRequiredNonAlphanumericCharacters
        {
            get { return 0; }
        }
        public override int MinRequiredPasswordLength
        {
            get { return 2; }
        }
        public override int PasswordAttemptWindow
        {
            get { throw new NotImplementedException(); }
        }
        public override MembershipPasswordFormat PasswordFormat
        {
            get { throw new NotImplementedException(); }
        }
        public override string PasswordStrengthRegularExpression
        {
            get { throw new NotImplementedException(); }
        }
        public override bool RequiresQuestionAndAnswer
        {
            get { throw new NotImplementedException(); }
        }
        public override bool RequiresUniqueEmail
        {
            get { throw new NotImplementedException(); }
        }
        public override string ResetPassword(string username, string answer)
        {
            throw new NotImplementedException();
        }
        public override bool UnlockUser(string userName)
        {
            throw new NotImplementedException();
        }
        public override void UpdateUser(MembershipUser user)
        {
            throw new NotImplementedException();
        }
    }
}